<?php
/**
 * @author fld 2021-09-11
 * @desc 图片控制类
 */
namespace App\Http\Controllers\Image;

use App\Service\Image\ImageService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ImageController extends Controller
{
    /**
     * @author fld 2021-09-11
     * @desc 图片弹出框
     * @param Request $request
     * @param ImageService $imageService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request, ImageService $imageService){
        $data = [];
        //分组列表
        $group_list = $imageService->getImgGroupList();
        $data['group_list'] = $group_list;
        if(empty($group_list)){
            $group_id = 0;
        }else{
            $group_id = $group_list[0]['id'];
        }
        //图片列表
        $img_result = $imageService->getImgList($group_id);
        $data['img_list'] = $img_result['img_list'];
        $data['pager'] = $img_result['pager'];
        //查询参数
        $data['params'] = request()->all();
        $data['params']['group_id'] = $img_result['group_id'];
        return view('image/index', $data);
    }
    /**
     * @author fld 2021-09-11
     * @desc 添加图片分组
     * @param Request $request
     * @param ImageService $imageService
     * @return \Illuminate\Http\JsonResponse|unknown
     */
    public function add_group(Request $request, ImageService $imageService){
       if($request->isMethod('POST')){
           $result = $imageService->add_group();
           if($result['code'] == 0){
               return response()->json(['status' => true,'message'=> $result['message']]);
           }else{
               return response()->json(['status' => false,'message'=> $result['message']]);
           }
       }else{
           $id = $request->post('id');
           $item = [];
           if(!empty($id)){
               $item = $imageService->getImgGroupByCondition(['id'=>$id]);
           }
           return view('image/add_group', ['item'=>$item]);
       }
    }
    
    
    /**
     * @author fld 2021-09-11
     * @desc 删除图片分组信息
     * @param ImageService $imageService
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete_group(Request $request, ImageService $imageService){
        $id = request()->post('id');
        $result = $imageService->delete_group($id);
        if($result['code'] == 0){
            return response()->json(['status' => true,'message'=> $result['message']]);
        }else{
            return response()->json(['status' => false,'message'=> $result['message']]);
        }
    }
    
    /**
     * @author fld 2021-09-14
     * @desc 更新图片分组排序
     * @param ImageService $imageService
     * @return \Illuminate\Http\JsonResponse
     */
    public function update_group(Request $request, ImageService $imageService){
        $result = $imageService->update_group();
        if($result['code'] == 0){
            return response()->json(['status' => true]);
        }else{
            return response()->json(['status' => false]);
        }
    }
    /**
     * @author fld 2021-09-13
     * @desc 上传图片
     * @param Request $request
     * @param ImageService $imageService
     * @return \Illuminate\Http\JsonResponse
     */
    public function upload(Request $request, ImageService $imageService){
        $result = $imageService->uploadImg($request);
        if($result['code'] == 0){
            return response()->json(['status' => true,'message'=> $result['message']]);
        }else{
            return response()->json(['status' => false,'message'=> $result['message']]);
        }
    }
    
    /**
     * @author fld 2021-09-13
     * @desc 删除图片
     * @param ImageService $imageService
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete_img(Request $request, ImageService $imageService){
        $id = request()->post('id');
        $result = $imageService->delete_img($id);
        if($result['code'] == 0){
            return response()->json(['status' => true,'message'=> $result['message']]);
        }else{
            return response()->json(['status' => false,'message'=> $result['message']]);
        }
    }
    
}
